package com.ui;

import java.awt.EventQueue;
import java.sql.Connection;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.controlador.Conexion;
import com.controlador.Operaciones;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;

public class RegistroBusqueda extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegistroBusqueda frame = new RegistroBusqueda();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegistroBusqueda() {
		setTitle("Search by...");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 150);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		//SE CREAN LOS BOTONES
		JRadioButton rSerial = new JRadioButton("Serial");
		JRadioButton rModel = new JRadioButton("Model");
		JRadioButton rFormType = new JRadioButton("Form Type");
		JRadioButton rProcess = new JRadioButton("Process");
		JRadioButton rEngineer = new JRadioButton("Engineer");
		JRadioButton rUser = new JRadioButton("User");
		//SE AGREGAN LOS ACTION COMMAND
		rSerial.setActionCommand("noSerie");
		rModel.setActionCommand("modelo");
		rFormType.setActionCommand("tipo");
		rProcess.setActionCommand("proceso");
		rEngineer.setActionCommand("idIngeniero");
		rUser.setActionCommand("usuario");
		//SE CREA EL GRUPO Y SE AGREGAN AL MISMO
		final ButtonGroup searchOptions = new ButtonGroup();
		searchOptions.add(rEngineer);
		searchOptions.add(rUser);
		searchOptions.add(rProcess);
		searchOptions.add(rFormType);
		searchOptions.add(rModel);
		searchOptions.add(rSerial);
		
		
		final JTextField txtBuscar = new JTextField();
		txtBuscar.setColumns(10);
		
		JButton btnBuscar = new JButton("Find");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				//Se genera la coneccion a la DB0
				Conexion connex = new Conexion();
				Connection conn = null;
				Operaciones op = new Operaciones();
				conn=connex.AccederBD();
				
					ButtonModel b = searchOptions.getSelection();
					String opc = b.getActionCommand();
					ResultadoBusqueda rb = new ResultadoBusqueda();
					String[] encabezado = {"ID","Serial #","Model","Form Type","Process","Reuse","Engineer","User","Date"}; 
					String [][] dataObj =(String[][]) op.buscar("equipos", "id,noSerie,modelo,tipo,proceso,reuso,idIngeniero,usuario,fecha",opc,txtBuscar.getText(), conn); 
					if(dataObj==null)
					{
						JOptionPane.showMessageDialog(null, "no results were found");
					}else{
					
					rb.setEncabezado(encabezado);
					rb.setDatos(dataObj);
					rb.populateInfo();					
					rb.setVisible(true);				
					System.err.println("si conecto y busco!!");
					}
	
			}
		});
		
		
		
		JLabel lblSearchBy = new JLabel("Search by");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(rSerial)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rModel)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rFormType))
								.addComponent(txtBuscar, Alignment.TRAILING))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rProcess)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rEngineer)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rUser))
								.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
									.addComponent(btnBuscar)
									.addGap(28))))
						.addComponent(lblSearchBy))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addComponent(lblSearchBy)
					.addGap(3)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(rSerial)
						.addComponent(rModel)
						.addComponent(rFormType)
						.addComponent(rProcess)
						.addComponent(rEngineer)
						.addComponent(rUser))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addComponent(txtBuscar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(24, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnBuscar)
							.addContainerGap())))
		);
		
		contentPane.setLayout(gl_contentPane);
		
	}
}
